package com.forestar.xzk.file;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * @author Administrator by xzk
 * @data 2020/7/23  15:43
 * @email SharedperfenceUtils
 * @remark   sharedPerfences工具类
 *
 */
public class SharedperfenceUtils {
    /** app的sp文件名  **/
    private static String SP_FILE_NAME = "appInit"; 
    private static Context appContext = null;

    private static SharedperfenceUtils instance;
    /**
      *Administrator
      *2020/7/23
      * 初始化sp操作对象 
      **/
    public static synchronized SharedperfenceUtils getInstance(){
        if(instance == null){
            instance = new SharedperfenceUtils();
        }
        return instance;
    }

    /**
     * 初始化方法，初始化本sp的环境，
     * @param fileName
     * @param context
     */
    public void initFileName(String fileName,Context context){
        SharedperfenceUtils.SP_FILE_NAME = fileName;
        SharedperfenceUtils.appContext = context;
    }

    public SharedPreferences getSharedPreferences(Context context){
        return context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
    }

    /**
     * 同步存储
     * @param key
     * @param value
     */
    public void setIntValue(String key, int value){
        SharedPreferences mSharedPreferences = getSharedPreferences(SharedperfenceUtils.appContext);
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key,value);
        editor.commit();
    }

    /**
     * 异步存储
     * @param key
     * @param value
     */
    public void setIntValueAsync(String key, int value){
        SharedPreferences mSharedPreferences = getSharedPreferences(SharedperfenceUtils.appContext);
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key,value);
        editor.apply();
    }

    public void setLongValue(String key, long value){
        SharedPreferences mSharedPreferences = getSharedPreferences(SharedperfenceUtils.appContext);
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putLong(key,value);
        editor.apply();
    }

    public void setBooleanValue(String key, boolean value){
        SharedPreferences mSharedPreferences = getSharedPreferences(SharedperfenceUtils.appContext);
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean(key,value);
        editor.apply();
    }

    /**
     * 保存字符串
     * @param key
     * @param value
     */
    public void setStringValue(String key, String value){
        SharedPreferences mSharedPreferences = getSharedPreferences(SharedperfenceUtils.appContext);
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(key,value);
        editor.commit();
    }

    /**
     * 保存字符串，异步
     * @param key
     * @param value
     */
    public void setStringValueAsync(String key, String value){
        SharedPreferences mSharedPreferences = getSharedPreferences(SharedperfenceUtils.appContext);
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(key,value);
        editor.apply();
    }

    public int getIntValue(String key){
        return getSharedPreferences(SharedperfenceUtils.appContext).getInt(key, 0);
    }
    public boolean getBooleanValue(String key){
        return getSharedPreferences(SharedperfenceUtils.appContext).getBoolean(key,false);
    }
    public Long getLongValue(String key){
        return getSharedPreferences(SharedperfenceUtils.appContext).getLong(key, -1);
    }

    public String getStringValue(String key){
        return getSharedPreferences(SharedperfenceUtils.appContext).getString(key, null);
    }
}
